package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.dao.EnergyViewProviderDao;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class EnergyViewProviderDaoImpl implements EnergyViewProviderDao {
    private String server="server";

    @Override
    public Map<String, String> findBusinessIds() {
        String sql="SELECT  group_concat(id) as ids from business";
        Map<String, String> map = JdbcUtil.queryForMap(sql, null);
        return map;
    }

    @Override
    public Map<String, String> findGpuSNs() {
        String sql="SELECT group_concat(device_id) as device_id FROM device_common WHERE device_model = ? and status =1";
        Object[] kk = new Object[1];
        kk[0] ="G58V";
        Map<String, String> map = JdbcUtil.queryForMap(sql, kk);
        return map;
    }

    @Override
    public boolean insertServerAmount(String business, int cpu_amount, int gpu_amount, int server_amount, Date collect_time, int business_id) {
        String sql = "insert into device_server_collect_amount(business,cpu_amount,gpu_amount,server_amount,collect_time,business_id) values (?,?,?,?,?,?)";
        Object[] kk = new Object[6];
        kk[0] =business;
        kk[1] =cpu_amount;
        kk[2] =gpu_amount;
        kk[3] =server_amount;
        kk[4] =collect_time;
        kk[5] =business_id;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public List<Map<String, String>> findServerModelAmount(int business_id, String startTime, String endTime) {
        String sql = "SELECT id,business,cpu_amount,gpu_amount,server_amount,collect_time,business_id FROM device_server_collect_amount WHERE business_id = ? AND  collect_time BETWEEN ? AND ? ";
        Object[] params = new Object[3];
        params[0] = business_id;
        params[1] = startTime;
        params[2] = endTime;
        List<Map<String, String>> maps = JdbcUtil.queryForList_Map(sql, params);
        return maps;
    }

    @Override
    public List<Map<String, String>> findAirRecommendTemp(String startTime, String endTime) {
        String sql = "SELECT * FROM d_recommend_temp WHERE collect_time BETWEEN ? AND ? ";
        Object[] params = new Object[2];
        params[0] = startTime;
        params[1] = endTime;
        List<Map<String, String>> maps = JdbcUtil.queryForList_Map(sql, params);
        return maps;
    }

    @Override
    public Map<String, String> totalServerAmount() {
        String sql="SELECT COUNT(1) as count from device_common where status=1 and type= ?";
        Object[] kk = new Object[1];
        kk[0] =server;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public boolean insertEnergyEfficiencyRatio(double energy_efficiency_ratio, String start_time, String end_time ,String cycle) {
        String sql = "insert into energy_efficiency_ratio_history(energy_efficiency_ratio,start_time,end_time,cycle) values (?,?,?,?)";
        Object[] kk = new Object[4];
        kk[0] =energy_efficiency_ratio;
        kk[1] =start_time;
        kk[2] =end_time;
        kk[3] =cycle;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public List<Map<String, String>> findEnergyEfficiencyRatioByCycle(String cycle) {
        String sql = "SELECT id,start_time,energy_efficiency_ratio,end_time,cycle FROM energy_efficiency_ratio_history where cycle = ? order by id desc limit 6 ";
        Object[] kk = new Object[1];
        kk[0] =cycle;
        List<Map<String, String>> maps = JdbcUtil.queryForList_Map(sql, kk);
        return maps;
    }

    @Override
    public Map<String, String> findIpBySn(String sn) {
        String sql="SELECT ip from device_common where device_id = ? and type= ? and status = 1";
        Object[] kk = new Object[2];
        kk[0] =sn;
        kk[1] =server;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public Map<String, String> findStrategyIdBySn(String sn) {
        String sql="SELECT strategyID from strategy_device where deviceID = ? ";
        Object[] kk = new Object[1];
        kk[0] =sn;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public Map<String, String> findStrategyNameById(String id) {
        String sql="SELECT strategyName from strategy where strategyID = ? ";
        Object[] kk = new Object[1];
        kk[0] =id;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public List<Map<String, String>> findQpsByBusiness(Integer business_id, String startTime, String endTime) {
        String sql = "SELECT id,business_name,business_id,collect_time,throughput FROM k8s_metrics WHERE business_id = ? AND  collect_time BETWEEN ? AND ? ";
        Object[] params = new Object[3];
        params[0] = business_id;
        params[1] = startTime;
        params[2] = endTime;
        List<Map<String, String>> maps = JdbcUtil.queryForList_Map(sql, params);
        return maps;
    }

    @Override
    public Map<String, String> findAvgQps(String startTime, String endTime) {
        String sql=" select  avg(cast(throughput as decimal(9,2))) as avg from k8s_metrics WHERE collect_time  BETWEEN ? AND ? limit 1";
        Object[] kk = new Object[2];
        kk[0] =startTime;
        kk[1] =endTime;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public Map<String, String> findAvgEer(String collectTimeStart,String collectTimeEnd, String cycle) {
        String sql=" select avg(energy_efficiency_ratio) as avg from energy_efficiency_ratio_history WHERE cycle = ? and start_time  BETWEEN ? AND ? ";
        Object[] kk = new Object[3];
        kk[0] =cycle;
        kk[1] =collectTimeStart;
        kk[2] =collectTimeEnd;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

    @Override
    public Map<String, String> findBusinessInfos(String business_id) {
        Object[] kk = new Object[1];
        kk[0] =business_id;
        String sql=" select * from business_collect_info where business_id = ? order by collect_time limit 1 ";
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        return map;
    }

}
